﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

// Use physics raycast hit from mouse click to set agent destination

//必须要有这个组件
[RequireComponent(typeof(NavMeshAgent))]
public class Clink : MonoBehaviour
{
    NavMeshAgent M_agent;
    public Vector3[] v = new Vector3[4];
    public GameObject plane;
    public float x;
    public float z;
    // Start is called before the first frame update
    void Start()
    {
        M_agent = GetComponent<NavMeshAgent>();
        // plane.GetComponent<RectTransform>().GetLocalCorners(v);
    }

    // Update is called once per frame
    void Update()
    {
        if (transform.name == "Cylinder")
        {
            x = Random.Range(v[0].x, v[3].x);
            z = Random.Range(v[0].y, v[1].y);
            M_agent.destination = new Vector3(x, transform.position.y, z);
        }
        else
        MouseDJ();
    }
    void MouseDJ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray.origin, ray.direction, out hit))
            {
                M_agent.destination = hit.point;
            }
        }
    }
}